home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 July: Mac OS SDK / Dev.CD Jul 96 SDK / Dev.CD Jul 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc / OpenDoc Development / Debugging Support / OpenDoc Source Code / Storage / LinkSpec.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-22  |  13.1 KB  |  434 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        LinkSpec.cpp
  3.  
  4.     Contains:    Implementation for ODLinkSpec class.
  5.  
  6.     Owned by:    Craig Carper
  7.  
  8.     Copyright:    © 1993 - 1995 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12.         <14>     10/8/95    TJ        Fixes Recomended by Refball
  13.         <13>     8/16/95    NP        1274946: ErrorDef.idl problems. Add include
  14.                                     file.
  15.         <12>     8/12/95    TÇ        1276812 Need to use TempObjs and TempRefs
  16.                                     for exception safety and to avoid TRY
  17.                                     blocks, 1276807 Opt./Bug: use StdTypIO
  18.                                     routines for portable streaming & smaller
  19.                                     footprint
  20.         <11>      8/3/95    RR        #1257260: Collapse B classes. Remove
  21.                                     somInit methods. Don't call IsInitialized
  22.                                     or SubclassResponsibility
  23.         <10>     5/26/95    VL        1251403: Multithreading naming support.
  24.          <9>     4/27/95    CC        1242555: GetOriginatingProcessID:  Removed
  25.                                     obsolete $5 comment.
  26.          <8>     3/10/95    CC        1225926:  Fixed typo in FromThisDraft().
  27.          <7>     1/27/95    TJ        Changed ODAppleEvent to AppleEvent.
  28.          <6>    12/20/94    VL        1195012: Make Storage calls be
  29.                                     marshallable.
  30.          <5>    12/15/94    CC        1205709 LinkSpec.cpp - Removed AcquirePart(),
  31.                                     GetData(), and GetDataSize() methods;
  32.                                     changed ReadLinkSpec(), WriteLinkSpec(),
  33.                                     ReadFromAppleEvent(), and
  34.                                     WriteToAppleEvent().
  35.          <4>     12/8/94    CC        1203864 WriteToAppleEvent: changed &_fData
  36.                                     to _fData, to copy the link data correctly.
  37.          <3>     9/15/94    CC        1186775 - Added SOM_CATCH to
  38.                                     FromThisDraft(); Return som error from
  39.                                     WriteToAppleEvent().
  40.          <2>     7/27/94    CC        Removed GetProcessLaunchDate, added
  41.                                     FromThisDraft, changed GetProcessID to
  42.                                     GetOriginatingProcessID.
  43.          <1>     6/21/94    CC        first checked in
  44.  
  45.         ------------------- CC ---- Converted to SOM
  46.  
  47.          <2>      5/9/94    MB        #1162181: Changes necessary to install MMM.
  48.          <1>     3/25/94    CC        first checked in
  49.  
  50.         ------------------- CC ---- Moved from Core to Storage subsystem
  51.  
  52.          <3>     3/24/94    CC        Changed WriteToProperty() to
  53.                                     WriteLinkSpec(), ReadFromProperty() to
  54.                                     ReadLinkSpec() (1153049); write version in
  55.                                     WriteToAppleEvent().
  56.          <2>     3/15/94    MB        Changes to support SCpp/ASLM builds,
  57.                                     #1150864.
  58.          <2>      2/8/94    TÇ        Throw -> THROW & some code clean up
  59.          <1>     1/21/94    CC        first checked in
  60.          <4>    12/21/93    VL        Changed XMPStorageUnit::GetValueSize to
  61.                                     StorageUnit::GetSize.
  62.          <3>     12/8/93    CC        Added WriteToAppleEvent,
  63.                                     ReadFromAppleEvent, and accessors for
  64.                                     fields.
  65.          <2>     12/3/93    TÇ        Stop including XMPError.h, it is included
  66.                                     as ErrorDef.h inside Except.h
  67.          <1>    11/15/93    CC        first checked in
  68.     To Do:
  69. */
  70.  
  71. #define VARIABLE_MACROS
  72.  
  73. #define ODLinkSpec_Class_Source
  74. #include <LinkSpec.xih>
  75.  
  76. #ifndef SOM_Module_OpenDoc_StdProps_defined
  77. #include "StdProps.xh"
  78. #endif
  79.  
  80. #ifndef SOM_Module_OpenDoc_StdTypes_defined
  81. #include "StdTypes.xh"
  82. #endif
  83.  
  84. #ifndef SOM_ODDraft_xh
  85. #include "Draft.xh"
  86. #endif
  87.  
  88. #ifndef SOM_ODStorageUnit_xh
  89. #include "StorageU.xh"
  90. #endif
  91.  
  92. #ifndef _EXCEPT_
  93. #include "Except.h"
  94. #endif
  95.  
  96. #ifndef _ODMEMORY_
  97. #include "ODMemory.h"
  98. #endif
  99.  
  100. #ifndef __ERRORS__
  101. #include <Errors.h>
  102. #endif
  103.  
  104. #ifndef _BARRAY_
  105. #include <BArray.h>
  106. #endif
  107.  
  108. #ifndef _STDTYPIO_
  109. #include <StdTypIO.h>
  110. #endif
  111.  
  112. #ifndef _TEMPOBJ_
  113. #include <TempObj.h>
  114. #endif
  115.  
  116. #ifndef _STORUTIL_
  117. #include <StorUtil.h>
  118. #endif
  119.  
  120. #ifndef _UTILERRS_
  121. #include "UtilErrs.h"
  122. #endif
  123.  
  124. #pragma segment ODLinkSpec
  125.  
  126. //==============================================================================
  127. // Constants
  128. //==============================================================================
  129. const ODULong kLinkSpecVersion = 1;    // Second version
  130.  
  131. // •••• kODSourcePartKey also defined for Drag & Drop ••••
  132. // •••• These don't belong here ••••
  133. #define kODProcessLaunchDateKey    'pldt'
  134. #define kODSourcePartKey            'part'
  135. #define kODLinkSpecDataKey            'data'
  136. #define kODLinkSpecDataSizeKey        'size'
  137. #define kODLinkSpecVersionKey        'vers'
  138.  
  139. //==============================================================================
  140. // ODLinkSpec
  141. //==============================================================================
  142.  
  143. //------------------------------------------------------------------------------
  144. // ODLinkSpec::somUninit
  145. //------------------------------------------------------------------------------
  146.  
  147. SOM_Scope void  SOMLINK ODLinkSpecsomUninit(ODLinkSpec *somSelf)
  148. {
  149.     /* ODLinkSpecData *somThis = ODLinkSpecGetData(somSelf); */
  150.     ODLinkSpecMethodDebug("ODLinkSpec","somUninit");
  151.  
  152.     parent_somUninit(somSelf);
  153. }
  154.  
  155. //------------------------------------------------------------------------------
  156. // ODLinkSpec::InitLinkSpec
  157. //------------------------------------------------------------------------------
  158.  
  159. SOM_Scope void  SOMLINK ODLinkSpecInitLinkSpec(ODLinkSpec *somSelf, Environment *ev,
  160.         ODPart* part,
  161.         ODByteArray* partData)
  162. {
  163.     ODLinkSpecData *somThis = ODLinkSpecGetData(somSelf);
  164.     ODLinkSpecMethodDebug("ODLinkSpec","InitLinkSpec");
  165.  
  166.     SOM_CATCH return;
  167.  
  168.     /* Moved from somInit. SOM itself sets fields to zero 
  169.     _fProcessLaunchDate = 0;
  170.     */
  171.     _fProcessID.lowLongOfPSN = kNoProcess;
  172.     _fProcessID.highLongOfPSN = 0;
  173.     
  174.     somSelf->InitBaseLinkSpec(ev, part, partData);
  175.  
  176.     if ( part )
  177.     {
  178.         ProcessSerialNumber psn;
  179.         ProcessInfoRec myProcessInfo;
  180.         
  181.         psn.lowLongOfPSN = kCurrentProcess;
  182.         psn.highLongOfPSN = 0;
  183.         myProcessInfo.processAppSpec = NULL;
  184.         myProcessInfo.processName = NULL;
  185.         myProcessInfo.processInfoLength = sizeof(myProcessInfo);
  186.         
  187.         OSErr error = GetProcessInformation(&psn, &myProcessInfo);
  188.         ASSERT((error == 0), kODErrAssertionFailed);
  189.     
  190.         _fProcessID = myProcessInfo.processNumber;
  191.         _fProcessLaunchDate = myProcessInfo.processLaunchDate;
  192.     }
  193. }
  194.  
  195. //------------------------------------------------------------------------------
  196. // ODLinkSpec::FromThisDraft (OVERRIDE)
  197. //------------------------------------------------------------------------------
  198.  
  199. SOM_Scope ODBoolean  SOMLINK ODLinkSpecFromThisDraft(ODLinkSpec *somSelf, Environment *ev)
  200. {
  201.     ODLinkSpecData *somThis = ODLinkSpecGetData(somSelf);
  202.     ODLinkSpecMethodDebug("ODLinkSpec","FromThisDraft");
  203.  
  204.     SOM_CATCH return kODFalse;
  205.     
  206.     ProcessSerialNumber myPSN;
  207.     ProcessInfoRec myProcessInfo;
  208.     Boolean isSamePSN;
  209.     
  210.     myPSN.lowLongOfPSN = kCurrentProcess;
  211.     myPSN.highLongOfPSN = 0;
  212.     myProcessInfo.processAppSpec = NULL;
  213.     myProcessInfo.processName = NULL;
  214.     myProcessInfo.processInfoLength = sizeof(myProcessInfo);
  215.     THROW_IF_ERROR(GetProcessInformation(&myPSN, &myProcessInfo));
  216.     THROW_IF_ERROR(SameProcess(&_fProcessID, &myProcessInfo.processNumber, &isSamePSN));
  217.  
  218.     return (isSamePSN && (myProcessInfo.processLaunchDate == _fProcessLaunchDate));
  219. }
  220.  
  221. //------------------------------------------------------------------------------
  222. // ODLinkSpec::GetOriginatingProcessID
  223. //------------------------------------------------------------------------------
  224.  
  225. SOM_Scope void  SOMLINK ODLinkSpecGetOriginatingProcessID(ODLinkSpec *somSelf, Environment *ev,
  226.         ProcessSerialNumber *psn)
  227. {
  228.     ODLinkSpecData *somThis = ODLinkSpecGetData(somSelf);
  229.     ODLinkSpecMethodDebug("ODLinkSpec","GetProcessLaunchDate");
  230.  
  231.     ProcessInfoRec info;
  232.     
  233.     info.processAppSpec = NULL;
  234.     info.processName = NULL;
  235.     info.processInfoLength = sizeof(info);
  236.  
  237.     if ( (GetProcessInformation(&_fProcessID, &info) == noErr) &&
  238.             (info.processLaunchDate == _fProcessLaunchDate) )
  239.     {
  240.         *psn = _fProcessID;
  241.     }
  242.     else
  243.     {
  244.         psn->lowLongOfPSN = kNoProcess;
  245.         psn->highLongOfPSN = 0;
  246.         ODSetSOMException(ev, kODErrCannotGetExternalLink);
  247.     }
  248. }
  249.  
  250. //------------------------------------------------------------------------------
  251. // ODLinkSpec::WriteLinkSpec (OVERRIDE)
  252. //------------------------------------------------------------------------------
  253.  
  254. SOM_Scope void  SOMLINK ODLinkSpecWriteLinkSpec(ODLinkSpec *somSelf, Environment *ev,
  255.         ODStorageUnit* su)
  256. {
  257.     ODLinkSpecData *somThis = ODLinkSpecGetData(somSelf);
  258.     ODLinkSpecMethodDebug("ODLinkSpec","WriteLinkSpec");
  259.  
  260.     SOM_CATCH return;
  261.  
  262.     ODVolatile(su);
  263.  
  264.     ODULong version = kLinkSpecVersion;
  265.     ODULong size;
  266.  
  267.     ODSUForceFocus(ev, su, kODNULL, kODLinkSpec);
  268.     size = su->GetSize(ev);
  269.     su->DeleteValue(ev, size);
  270.  
  271.     TRY
  272.         StorageUnitSetValue(su, ev, sizeof(ODULong), (ODValue) &version);
  273.         StorageUnitSetValue(su, ev, sizeof(ProcessSerialNumber), (ODValue) &_fProcessID);
  274.         StorageUnitSetValue(su, ev, sizeof(ODULong), (ODValue) &_fProcessLaunchDate);
  275.  
  276.         parent_WriteLinkSpec(somSelf, ev, su);
  277.     CATCH_ALL
  278.         su->Remove(ev);
  279.         RERAISE;
  280.     ENDTRY
  281. }
  282.  
  283. //------------------------------------------------------------------------------
  284. // ODLinkSpec::ReadLinkSpec (OVERRIDE)
  285. //------------------------------------------------------------------------------
  286.  
  287. SOM_Scope void  SOMLINK ODLinkSpecReadLinkSpec(ODLinkSpec *somSelf, Environment *ev,
  288.         ODStorageUnit* su)
  289. {
  290.     ODLinkSpecData *somThis = ODLinkSpecGetData(somSelf);
  291.     ODLinkSpecMethodDebug("ODLinkSpec","ReadLinkSpec");
  292.  
  293.     SOM_CATCH return;
  294.  
  295.     ODULong                version;
  296.     ProcessSerialNumber processID;
  297.     ODULong                processLaunchDate;
  298.     ODULong                valueSize;
  299.  
  300.     // Pre-focused property must contain a link spec value
  301.     if (!ODSUExistsThenFocus(ev, su, (ODPropertyName) kODNULL, kODLinkSpec))
  302.         THROW(kODErrNoLinkSpecValue);
  303.  
  304.     StorageUnitGetValue(su, ev, sizeof(ODULong), (ODValue) &version);
  305.     if (version != kLinkSpecVersion) 
  306.         THROW(kODErrUnknownLinkSpecVersion);
  307.  
  308.     valueSize = StorageUnitGetValue(su, ev, sizeof(ProcessSerialNumber), (ODValue) &processID);
  309.     if ( valueSize != sizeof(ProcessSerialNumber) )
  310.         THROW(kODErrCorruptLinkSpecValue);
  311.  
  312.     valueSize = StorageUnitGetValue(su, ev, sizeof(ODULong), (ODValue) &processLaunchDate);
  313.     if ( valueSize != sizeof(ODULong) )
  314.         THROW(kODErrCorruptLinkSpecValue);
  315.  
  316.     parent_ReadLinkSpec(somSelf, ev, su);
  317.  
  318.     _fProcessID = processID;
  319.     _fProcessLaunchDate = processLaunchDate;
  320. }
  321.  
  322. //------------------------------------------------------------------------------
  323. // ODLinkSpec::WriteToAppleEvent
  324. //------------------------------------------------------------------------------
  325.  
  326. SOM_Scope void  SOMLINK ODLinkSpecWriteToAppleEvent(ODLinkSpec *somSelf, Environment *ev,
  327.         AppleEvent* theAppleEvent)
  328. {
  329.     ODLinkSpecData *somThis = ODLinkSpecGetData(somSelf);
  330.     ODLinkSpecMethodDebug("ODLinkSpec","WriteToAppleEvent");
  331.     
  332.     SOM_TRY
  333.  
  334.     OSErr error;
  335.     ODPart* part = somSelf->GetPart(ev);
  336.     ODByteArray partData = somSelf->GetPartData(ev);
  337.  
  338.     ODULong version = kLinkSpecVersion;
  339.     error = AEPutParamPtr((AppleEvent*) theAppleEvent, kODLinkSpecVersionKey, typeLongInteger,
  340.                         &version, sizeof(version));
  341.  
  342.     if ( error == noErr )
  343.         error = AEPutParamPtr((AppleEvent*) theAppleEvent, keyProcessSerialNumber, typeProcessSerialNumber,
  344.                             &_fProcessID, sizeof(_fProcessID));
  345.  
  346.     if ( error == noErr )
  347.         error = AEPutParamPtr((AppleEvent*) theAppleEvent, kODProcessLaunchDateKey, typeLongInteger,
  348.                             &_fProcessLaunchDate, sizeof(_fProcessLaunchDate));
  349.  
  350.     if ( error == noErr )
  351.         error = AEPutParamPtr((AppleEvent*) theAppleEvent, kODSourcePartKey, typeLongInteger,
  352.                             &part, sizeof(part));
  353.  
  354.     if ( error == noErr )
  355.         error = AEPutParamPtr((AppleEvent*) theAppleEvent, kODLinkSpecDataSizeKey, typeLongInteger,
  356.                             &partData._length, sizeof(ODULong));
  357.  
  358.     if ( (error == noErr) && (partData._length > 0) )
  359.         error = AEPutParamPtr((AppleEvent*) theAppleEvent, kODLinkSpecDataKey, typeChar /* ••• need better type here •••• */,
  360.                             partData._buffer, partData._length);
  361.     
  362.     DisposeByteArrayStruct(partData);
  363.  
  364.     THROW_IF_ERROR(error);
  365.     
  366.     SOM_CATCH_ALL
  367.     SOM_ENDTRY
  368. }
  369.  
  370. //------------------------------------------------------------------------------
  371. // ODLinkSpec::ReadFromAppleEvent
  372. //------------------------------------------------------------------------------
  373.  
  374. SOM_Scope void  SOMLINK ODLinkSpecReadFromAppleEvent(ODLinkSpec *somSelf, Environment *ev,
  375.         AppleEvent* theAppleEvent)
  376. {
  377.     ODLinkSpecData *somThis = ODLinkSpecGetData(somSelf);
  378.     ODLinkSpecMethodDebug("ODLinkSpec","ReadFromAppleEvent");
  379.  
  380.     SOM_CATCH return;
  381.  
  382.     /* Moved from somInit. SOM itself sets fields to zero 
  383.     _fProcessLaunchDate = 0;
  384.     _fProcessID.lowLongOfPSN = kNoProcess;
  385.     _fProcessID.highLongOfPSN = 0;
  386.     */
  387.     
  388.     OSErr                result;
  389.     DescType            returnedType;
  390.     Size                actualSize;
  391.     ODPart*                part;
  392.     ODByteArray            partData;
  393.     ProcessSerialNumber processID;
  394.     ODULong                processLaunchDate;
  395.     ODULong                dataSize;
  396.  
  397.     result = AEGetParamPtr((AppleEvent*) theAppleEvent, keyProcessSerialNumber, typeProcessSerialNumber, &returnedType,
  398.                             &processID, sizeof(processID), &actualSize);
  399.     THROW_IF_ERROR(result);
  400.  
  401.     result = AEGetParamPtr((AppleEvent*) theAppleEvent, kODProcessLaunchDateKey, typeLongInteger, &returnedType,
  402.                             &processLaunchDate, sizeof(processLaunchDate), &actualSize);
  403.     THROW_IF_ERROR(result);
  404.  
  405.     result = AEGetParamPtr((AppleEvent*) theAppleEvent, kODSourcePartKey, typeLongInteger, &returnedType,
  406.                             &part, sizeof(part), &actualSize);
  407.     THROW_IF_ERROR(result);
  408.  
  409.     result = AEGetParamPtr((AppleEvent*) theAppleEvent, kODLinkSpecDataSizeKey, typeLongInteger, &returnedType,
  410.                             &dataSize, sizeof(ODULong), &actualSize);
  411.     THROW_IF_ERROR(result);
  412.     
  413.     partData = CreateEmptyByteArrayStruct(dataSize);
  414.  
  415.     TRY
  416.         if ( dataSize > 0 )
  417.         {
  418.             result = AEGetParamPtr((AppleEvent*) theAppleEvent, kODLinkSpecDataKey, typeChar, &returnedType,
  419.                                     partData._buffer, dataSize, &actualSize);
  420.             THROW_IF_ERROR(result);
  421.             partData._length = dataSize;
  422.         }
  423.         somSelf->InitBaseLinkSpec(ev, part, &partData);
  424.     CATCH_ALL
  425.         DisposeByteArrayStruct(partData);
  426.         RERAISE;
  427.     ENDTRY
  428.  
  429.     DisposeByteArrayStruct(partData);
  430.     
  431.     _fProcessID = processID;
  432.     _fProcessLaunchDate = processLaunchDate;
  433. }
  434.